#include "bsp_timer.h"
#include "bsp_led.h"
#include "bsp_can.h"

boolean_t  PULSE_5V_ON_Flag = FALSE;
boolean_t  PULSE_12V_ON_Flag = FALSE;

//Timer0配置初始化
void BSP_Timer0_init(uint16_t u16Period)
{
    uint16_t                 u16ArrValue;
    uint16_t                 u16CntValue;
    stc_bt_mode0_cfg_t       stcBtBaseCfg;
    
    DDL_ZERO_STRUCT(stcBtBaseCfg);
    
    Sysctrl_SetPeripheralGate(SysctrlPeripheralBaseTim, TRUE); //Base Timer外设时钟使能
    
    stcBtBaseCfg.enWorkMode = BtWorkMode0;                  //定时器模式
    stcBtBaseCfg.enCT       = BtTimer;                      //定时器功能，计数时钟为内部PCLK
    stcBtBaseCfg.enPRS      = BtPCLKDiv16;                 //PCLK/16
    stcBtBaseCfg.enCntMode  = Bt16bitArrMode;               //自动重载16位计数器/定时器
    stcBtBaseCfg.bEnTog     = FALSE;
    stcBtBaseCfg.bEnGate    = FALSE;
    stcBtBaseCfg.enGateP    = BtGatePositive;
    Bt_Mode0_Init(TIM0, &stcBtBaseCfg);                     //TIM0 的模式0功能初始化
    
    u16ArrValue = 0x10000 - u16Period;
    Bt_M0_ARRSet(TIM0, u16ArrValue);                        //设置重载值(ARR = 0x10000 - 周期)
    
    u16CntValue = 0x10000 - u16Period;
    Bt_M0_Cnt16Set(TIM0, u16CntValue);                      //设置计数初值
    
    Bt_ClearIntFlag(TIM0,BtUevIrq);                         //清中断标志   
    Bt_Mode0_EnableIrq(TIM0);                               //使能TIM0中断(模式0时只有一个中断)
    EnableNvic(TIM0_IRQn, IrqLevel3, TRUE);                 //TIM0中断使能
	
	Bt_M0_Run(TIM0);        //TIM0 运行
}


//Timer1配置初始化
void BSP_Timer1_init(uint16_t u16Period)
{
    uint16_t                 u16ArrValue;
    uint16_t                 u16CntValue;
    stc_bt_mode0_cfg_t       stcBtBaseCfg;
    
    DDL_ZERO_STRUCT(stcBtBaseCfg);
    
    Sysctrl_SetPeripheralGate(SysctrlPeripheralBaseTim, TRUE); //Base Timer外设时钟使能
    
    stcBtBaseCfg.enWorkMode = BtWorkMode0;                  //定时器模式
    stcBtBaseCfg.enCT       = BtTimer;                      //定时器功能，计数时钟为内部PCLK
    stcBtBaseCfg.enPRS      = BtPCLKDiv64;                 //PCLK/64
    stcBtBaseCfg.enCntMode  = Bt16bitArrMode;               //自动重载16位计数器/定时器
    stcBtBaseCfg.bEnTog     = FALSE;
    stcBtBaseCfg.bEnGate    = FALSE;
    stcBtBaseCfg.enGateP    = BtGatePositive;
    Bt_Mode0_Init(TIM1, &stcBtBaseCfg);                     //TIM1 的模式0功能初始化
    
    u16ArrValue = 0x10000 - u16Period;
    Bt_M0_ARRSet(TIM1, u16ArrValue);                        //设置重载值(ARR = 0x10000 - 周期)
    
    u16CntValue = 0x10000 - u16Period;
    Bt_M0_Cnt16Set(TIM1, u16CntValue);                      //设置计数初值
    
    Bt_ClearIntFlag(TIM1,BtUevIrq);                         //清中断标志   
    Bt_Mode0_EnableIrq(TIM1);                               //使能TIM1中断(模式0时只有一个中断)
    EnableNvic(TIM1_IRQn, IrqLevel2, TRUE);                 //TIM1中断使能
	
	Bt_M0_Run(TIM1);        //TIM1 运行
}


void BSP_Timer3_init(uint16_t u16Period)
{
	uint16_t                    u16ArrValue;
    uint16_t                    u16CntValue;
    stc_tim3_mode0_cfg_t     stcTim3BaseCfg;
    
    //结构体初始化清零
    DDL_ZERO_STRUCT(stcTim3BaseCfg);
    
    Sysctrl_SetPeripheralGate(SysctrlPeripheralTim3, TRUE); //Base Timer外设时钟使能
    
    stcTim3BaseCfg.enWorkMode = Tim3WorkMode0;              //定时器模式
    stcTim3BaseCfg.enCT       = Tim3Timer;                  //定时器功能，计数时钟为内部PCLK
    stcTim3BaseCfg.enPRS      = Tim3PCLKDiv64;              //PCLK/64
    stcTim3BaseCfg.enCntMode  = Tim316bitArrMode;           //自动重载16位计数器/定时器
    stcTim3BaseCfg.bEnTog     = FALSE;
    stcTim3BaseCfg.bEnGate    = FALSE;
    stcTim3BaseCfg.enGateP    = Tim3GatePositive;
    
    Tim3_Mode0_Init(&stcTim3BaseCfg);                       //TIM3 的模式0功能初始化
        
    u16ArrValue = 0x10000 - u16Period;
    
    Tim3_M0_ARRSet(u16ArrValue);                            //设置重载值(ARR = 0x10000 - 周期)
    
    u16CntValue = 0x10000 - u16Period;
    
    Tim3_M0_Cnt16Set(u16CntValue);                          //设置计数初值
    
    Tim3_ClearIntFlag(Tim3UevIrq);                          //清中断标志
    Tim3_Mode0_EnableIrq();                                 //使能TIM3中断(模式0时只有一个中断)
    EnableNvic(TIM3_IRQn, IrqLevel3, TRUE);                 //TIM3 开中断 	
	
	Tim3_M0_Run();   //TIM3 运行
}

/*******************************************************************************
 * TIM3中断服务函数 (5ms)
 模式0 计时溢出中断
 ******************************************************************************/
void Tim3_IRQHandler(void)
{
    static uint32_t i = 10;
	static uint32_t j = 40;

    if(TRUE == Tim3_GetIntFlag(Tim3UevIrq))
    {	
        Tim3_ClearIntFlag(Tim3UevIrq);  //Timer3模式0 中断标志清除      
				Adc_SQR_Start(); /*5ms启动一次转换*/
				if((++i) >= 20)  /*100MS*/
				{
					i = 0;	
					APP_CAN.CANSendFlag = TRUE;
				}
				if((++j) >= 200)  /*1S*/
				{
					j = 0;
					APP_CAN.CAN_keepalive_SendFlag = TRUE;
					//BSP_led_blink(); //test
				}

				if((++APP_CAN.Rx_cnt) >= 200*30)  /*30S*/
				{			
		//			TJA1043_Goto_Sleep();
				}		
				APP_CAN.brake_cnt++;
				APP_CAN.key_cnt++;
				APP_CAN.key_press_cnt++;
				
    }
}

/*******************************************************************************
 * TIM1中断服务函数 2ms
 ******************************************************************************/
void Tim1_IRQHandler(void)
{
    static int i = 0;
	static int t = 0;
    //Timer1 模式0 溢出中断
    if(TRUE == Bt_GetIntFlag(TIM1, BtUevIrq))
    {
		Bt_ClearIntFlag(TIM1,BtUevIrq); //中断标志清零	
		if((i++)%2)
		{
			PULSE_5V_ON_Flag = TRUE;	
		}
		else
		{
			if((++t) >= 1000)  /* 2*2000 = 4s*/
			{
				PULSE_12V_ON_Flag = TRUE;
				t = 0;
			}		
		} 
		if(i >= 0x0fffffff)
		{
			i = 0;
		}
    }
}


